from rest_framework.views import APIView

from  . import models,Serializer,paginnations,filters

from django_filters.rest_framework import DjangoFilterBackend

from rest_framework.generics import ListAPIView
from rest_framework.filters import SearchFilter,OrderingFilter


#免费课程视图类
class FreeCourseAPIView(ListAPIView):
    queryset = models.Course.objects.filter(is_delete=False,is_show=True).order_by('orders').all()
    serializer_class = Serializer.FreeCourserListModelSerializer

    #过滤器
    filter_backends = [OrderingFilter,SearchFilter,DjangoFilterBackend]

    #参与排序的字段
    ordering_fields = ['price','id','students']

    #参与搜索的字段
    search_fields = ['name','brief']

    #区间搜索
    filter_class = filters.CourseFilterSet

    # 分页器
    pagination_class = paginnations.CoursePageNumberPagination

#查询所有的分类的课程
class CategoryListAPIView(ListAPIView):
    queryset = models.CourseCategory.objects.all()
    serializer_class = Serializer.CategoryModelSerializer

from rest_framework.generics import RetrieveAPIView
class FreeCourseRetrieveAPIView(RetrieveAPIView):
    queryset = models.Course.objects.filter(is_delete=False, is_show=True).order_by('-orders').all()
    serializer_class = Serializer.FreeCourseModelSerializer

#查询单个分类下的课程 分组筛选

# from django_filters.rest_framework import DjangoFilterBackend
class ChapterListAPIView(ListAPIView):
    queryset = models.CourseCategory.objects.filter(is_delete=False,is_show=True).all()
    serializer_class = Serializer.CourseChapterModelSerializer

    filter_backends = [DjangoFilterBackend]
    filter_fields = ['course']

